package com.jkd.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.jkd.model.pojo.Building;
import com.jkd.model.pojo.Discipline;
import com.jkd.model.pojo.Room;
import com.jkd.model.pojo.Student;
import com.jkd.model.vo.Result;
import com.jkd.model.vo.RoomQueryVo;
import com.jkd.service.IBuildingService;
import com.jkd.service.IDisciplineService;
import com.jkd.service.IRoomService;
import com.jkd.service.IStudentService;
import com.jkd.service.impl.BuildingServiceImpl;
import com.jkd.service.impl.DisciplineServiceImpl;
import com.jkd.service.impl.RoomServiceImpl;
import com.jkd.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/service/student/*")
public class StudentController extends HttpServlet {
    private IStudentService studentService = new StudentServiceImpl();
    private IDisciplineService disciplineService = new DisciplineServiceImpl();
    private IRoomService roomService = new RoomServiceImpl();
    private IBuildingService iBuildingService = new BuildingServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo =  req.getPathInfo();
        switch (pathInfo){
            case "/index":
                RoomQueryVo roomQueryVo = new RoomQueryVo();
                String pageNow = req.getParameter("pageNow");
                System.out.println(pageNow);
                pageNow = (pageNow == null)?"1":pageNow;
                //以下为分页显示的内容
                List<Building> buildingList = iBuildingService.selectAll();
                List<String> roomNumList  = roomService.selectByDistinctRoom();
                List<Integer> capacityList = roomService.selectByDistinctCapacity();
                 //多条件查询
                String count  = req.getParameter("count");
                if(count != null){//判断是否为第一次进入
                    String buildingId = req.getParameter("buildingId");
                    String roomNum  = req.getParameter("roomNum");
                    String capacity = req.getParameter("capacity");
                    String statusId = req.getParameter("statusId");
                    if(buildingId != null && buildingId.trim().length()>0){
                        roomQueryVo.setBuildingId(Integer.valueOf(buildingId));
                        req.setAttribute("building",buildingId);
                    }
                    if(roomNum != null && roomNum.trim().length()>0){
                        //下次为null时无法进入获取不会对值进行更改
                        roomQueryVo.setRoomNum(roomNum);
                        req.setAttribute("roomNum",roomNum);
                    }
                    if(capacity != null && capacity.trim().length()>0){
                        roomQueryVo.setCapacity(Integer.valueOf(capacity));
                        req.setAttribute("capacity",capacity);
                    }
                    if(statusId != null && statusId.trim().length()>0){
                        roomQueryVo.setStatusId(Integer.valueOf(statusId));
                        req.setAttribute("statusId",statusId);
                    }
                    req.setAttribute("count",count);
                }
                 PageInfo<Room> roomInfo= roomService.selectByAll(roomQueryVo,Integer.valueOf(pageNow),3);
                 roomInfo.getList().forEach(e-> System.out.println(e));
                req.setAttribute("buildingList",buildingList);
                req.setAttribute("roomNumList",roomNumList);
                req.setAttribute("capacityList",capacityList);
                req.setAttribute("roomInfo",roomInfo);
                req.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(req,resp);
                break;
            case  "/modify":
                List<Discipline> list = disciplineService.selectAll();
                    req.setAttribute("discipline",list);
                    req.getRequestDispatcher("/WEB-INF/jsp/stuInfo.jsp").forward(req,resp);
                break;

            case "/update":
                resp.setContentType("application/json;charset=utf-8");
                PrintWriter out  = resp.getWriter();
                req.setCharacterEncoding("utf-8");
                String disc = req.getParameter("discipline");
                String grade = req.getParameter("grade");
                String name = req.getParameter("name");
                String sex = req.getParameter("sex");
                String cel = req.getParameter("cel");
                String location = req.getParameter("location");
                Student student = new Student();
                HttpSession session = req.getSession();
                Student student1 = (Student) session.getAttribute("student");
                System.out.println(student);
                Result result = null;

                if (disc != null  &&grade != null  && name != null   && cel != null  &&location != null ){
                    student.setDisciplineId(Integer.valueOf(disc));
                    student.setGrade(Integer.valueOf(grade));
                    student.setName(name);
                    if("男".equals(sex)){
                        student.setSex(1);
                    }else if("女".equals(name)){
                        student.setSex(0);
                    }
                    student.setCel(cel);
                    student.setLocation(location);
                    student.setStuId(student1.getStuId());
                    studentService.update(student);

                    result = new Result("200","添加成功",student);

                }else {
                    result = new Result("500","添加失败",null);
                }
                out.print(JSON.toJSONString(result));
                 break;

            case "/echo":
                req.getRequestDispatcher("/WEB-INF/jsp/stuInfo.jsp").forward(req,resp);
                break;

        }

    }
}
